using System;
using System.Collections.Generic;
using System.Text;

namespace DirectorySerber
{

    // IP Not in List Exception
    class IPNotInListException : Exception
    {
        public IPNotInListException() { }
        public IPNotInListException(string message) : base(message) { }
    }

    class IPList
    {

        // Vars
        public Dictionary<int, string> List;
        public Dictionary<string, Node> ReverseList; // IPs backwards >.>
        public int Count
        {
            get
            {
                return List.Count;
            }
        }

        // Constructor
        public IPList()
        {
            List = new Dictionary<int, string>();
            ReverseList = new Dictionary<string, Node>();
        }

        // Add
        public void Add(string IP, int Port)
        {

            // Exist?
            if (!List.ContainsValue(IP))
            {

                // Find free slot
                for (int i = 0; i <= List.Count; i++)
                    if (!List.ContainsKey(i))
                    {

                        // List
                        List.Add(i, IP);

                        // Reverse List
                        ReverseList.Add(IP, new Node(i, Port));

                        // Done!
                        break;

                    }

            }

        }

        // Exist
        public bool Contains(int ID)
        {
            return List.ContainsKey(ID);
        }
        public bool Contains(string IP)
        {
            return ReverseList.ContainsKey(IP);
        }

        // Get IP
        public string GetIP(int ID)
        {
            // Just return; it's the coders fault if they don't check before hand
            // we don't know what to do if it doesn't exist!
            return List[ID];
        }

        // Get ID
        public int GetID(string IP)
        {
            // Same as above, coder's fault if they don't check.
            return ReverseList[IP].ID;
        }

        // Get Port
        public int GetPort(int ID)
        {
            return ReverseList[List[ID]].Port;
        }
        public int GetPort(string IP)
        {
            return ReverseList[IP].Port;
        }

        // Set Port
        public void SetPort(int ID, int Port)
        {

            // Check if it exists
            if (List.ContainsKey(ID))
                ReverseList[List[ID]].Port = Port;

        }
        public void SetPort(string IP, int Port)
        {

            // Check it exists
            if (ReverseList.ContainsKey(IP))
                ReverseList[IP].Port = Port;

        }

        // Delete
        public void Delete(int ID)
        {

            // We will check it exists here.
            if (List.ContainsKey(ID))
            {

                // Delete
                ReverseList.Remove(List[ID]);
                List.Remove(ID);

            }

        }
        public void Delete(string IP)
        {

            // We will check if it exists here.
            if (ReverseList.ContainsKey(IP))
            {

                // Delete
                List.Remove(ReverseList[IP].ID);
                ReverseList.Remove(IP);

            }

        }

    }
}
